package org.proddevplm.helpers;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class JSONParser {


    public static List<Map<String, String>> parse(String jsonMessage) {

        jsonMessage = jsonMessage.substring(1, jsonMessage.length() - 1);
        List<String> objects = new ArrayList<String>();
        int startIndex = 0;

        List<Map<String, String>> objectMap = new ArrayList<Map<String, String>>();

        for (int i = 0; i < jsonMessage.length(); i++) {
            if (jsonMessage.charAt(i) == '{') {
                startIndex = i + 1;
            }

            if (jsonMessage.charAt(i) == '}') {
                objects.add(jsonMessage.substring(startIndex, i));
            }
        }

        for (String object : objects) {
            objectMap.add(getFieldInfos(object));
        }

        return objectMap;
    }

    private static Map<String, String> getFieldInfos(String object) {
        String[] values = object.split(",");
        String[] indValue;
        String key;
        String value;
        Map<String, String> fields = new HashMap<String, String>(values.length);
        for (String val : values) {
            indValue = val.split(":");
            key = indValue[0].replaceAll("\"", "");
            value = indValue[1].replaceAll("\"", "");
            fields.put(key, value);
        }
        return fields;
    }


}


class Root {
    Map<String, Object> test;

    public Map<String, Object> getTest() {
        return test;
    }

    public void setTest(Map<String, Object> test) {
        this.test = test;
    }
}
